package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.dto.PageDTO;
import com.leyou.common.exception.LyException;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.entity.Sku;
import com.leyou.item.entity.Spu;
import com.leyou.item.mapper.SpuMapper;
import com.leyou.item.service.SKuService;
import com.leyou.item.service.SpuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j //日志
public class SpuServiceImpl extends ServiceImpl<SpuMapper, Spu> implements SpuService {

/*    @Autowired
    private SKuService skuService;*/
    @Override
    public PageDTO<SpuDTO> pageQuery(Integer page, Integer rows, Integer saleable) {

        // 分页条件对象
        Page<Spu> spuPage = new Page<>(page, rows);

        //执行分页查询及条件查询 spuPage,wrapper
        page(spuPage,
                new QueryWrapper<Spu>().eq(saleable != null,"saleable",saleable));
        List<SpuDTO> spuDTOS = SpuDTO.convertEntityList(spuPage.getRecords());// 查询数据列表

        //返回
        return new PageDTO<>(spuPage.getTotal(),spuPage.getPages(),spuDTOS);
    }



    /*    *//*商品上下架*//*
    @Override
    public void editSaleable(Long id, Boolean saleable) {

        // 修改 spu
        Spu spu = new Spu();
        spu.setId(id);
        spu.setSaleable(saleable);
        boolean success = updateById(spu);
        if (!success){
            throw new LyException(500,"更新失败");
        }

        // 修改sku
        //一个spu对应多个sku
        // 2.更新sku
       // success =skuService.update().eq("spu_id", id).set("saleable", saleable).update();
        if (!success) {
            throw new LyException(500, "更新失败");
        }

    }




    *//*新增商品*//*
    @Override
    @Transactional
    public void addGoods(SpuDTO spuDTO) {
        // 打印传递参数
        log.info(spuDTO.toString());
        // 新增spu

        // 新增spu deatial
        // 新增sku
    }*/


    /*
    * 修改商品信息 第一步 回显数据spu+spudeatil+sku
    * */
    @Override
    public SpuDTO queryGoodsBySpuID(Long id) {
        // 调用之前的分页查询的代码
        return null;
    }
}
